package com.eagledata.project.tool.gen.service;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.List;
import java.util.zip.ZipOutputStream;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eagledata.common.constant.Constants;
import com.eagledata.common.utils.StringUtil;
import com.eagledata.framework.config.GenConfig;
import com.eagledata.project.tool.gen.domain.ColumnInfo;
import com.eagledata.project.tool.gen.domain.TableInfo;
import com.eagledata.project.tool.gen.mapper.GenMapper;
import com.eagledata.project.tool.gen.util.GenUtils;
import com.eagledata.project.tool.gen.util.VelocityInitializer;

/**
 * 代码生成 服务层处理
 * 
 * @author ruoyi
 */
@Service("genService")
public class GenServiceImpl implements IGenService {
	@Autowired
	private GenMapper genMapper;

	/**
	 * 查询ry数据库表信息
	 * 
	 * @param tableInfo
	 *            表信息
	 * @return 数据库表列表	
	 */
	@Override
	public List<TableInfo> selectTableList(TableInfo tableInfo) {
		return genMapper.selectTableList(tableInfo);
	}

	/**
	 * 生成代码
	 * 
	 * @param tableName
	 *            表名称
	 * @return 数据
	 */
	@Override
	public byte[] generatorCode(String tableName) {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		ZipOutputStream zip = new ZipOutputStream(outputStream);
		// 查询表信息
		TableInfo table = genMapper.selectTableByName(tableName);
		// 查询列信息
		List<ColumnInfo> columns = genMapper.selectTableColumnsByName(tableName);
		// 生成代码
		generatorCode(table, columns, zip);
		IOUtils.closeQuietly(zip);
		return outputStream.toByteArray();
	}

	/**
	 * 批量生成代码
	 * 
	 * @param tableNames
	 *            表数组
	 * @return 数据
	 */
	@Override
	public byte[] generatorCode(String[] tableNames) {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		ZipOutputStream zip = new ZipOutputStream(outputStream);
		for (String tableName : tableNames) {
			// 查询表信息
			TableInfo table = genMapper.selectTableByName(tableName);
			// 查询列信息
			List<ColumnInfo> columns = genMapper.selectTableColumnsByName(tableName);
			// 生成代码
			generatorCode(table, columns, zip);
		}
		IOUtils.closeQuietly(zip);
		return outputStream.toByteArray();
	}

	/**
	 * 生成文件
	 * @param file
	 */
    public static void mkDir(File file) {  
        if (file.getParentFile().exists()) {  
            file.mkdir();  
        } else {  
            mkDir(file.getParentFile());  
            file.mkdir();    
        }  
    }  
	
	/**
	 * 生成代码
	 */
	public void generatorCode(TableInfo table, List<ColumnInfo> columns, ZipOutputStream zip) {
		// 表名转换成Java属性名
		String className = GenUtils.tableToJava(table.getTableName());
		table.setClassName(className);
		table.setClassname(StringUtil.uncapitalize(className));
		// 列信息
		table.setColumns(GenUtils.transColums(columns));
		// 设置主键
		table.setPrimaryKey(table.getColumnsLast());

		VelocityInitializer.initVelocity();

		String packageName = GenConfig.getPackageName();
		String moduleName = GenUtils.getModuleName(packageName);
		// 文件路径
		VelocityContext context = GenUtils.getVelocityContext(table);

		// 获取模板列表
		List<String> templates = GenUtils.getTemplates();
		for (String template : templates) {
			// 渲染模板
			StringWriter sw = new StringWriter();
			Template tpl = Velocity.getTemplate(template, Constants.UTF8);
			tpl.merge(context, sw);
			/*				// 添加到zip
			File file = new File(GenUtils.getFileName(template, table, moduleName));
			zip.putNextEntry(new ZipEntry(GenUtils.getFileName(template, table, moduleName)));
			IOUtils.write(sw.toString(), zip, Constants.UTF8);
			IOUtils.closeQuietly(sw);
			zip.closeEntry();*/
			String path = System.getProperty("user.dir") + File.separator  + "src" + File.separator + GenUtils.getFileName(template, table, moduleName);
			System.out.println(path);
			writeToFile(path, sw.toString(), true);
		}
	}
	
	
	public static void writeToFile(String fileName, String content, boolean append) {
		try {
			FileUtils.write(new File(fileName), content, "utf-8", append);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
